package com.shengqi.xinnongren.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.adapter.GeneralFragmentPagerAdapter;
import com.shengqi.xinnongren.common.base.BaseActivity;
import com.shengqi.xinnongren.work.ui.user.fragments.OrderListFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyOrderListActivity extends BaseActivity {

    public static final String EXTRA_TYPE = "type";

    @BindView(R.id.tl_class)
    TabLayout mTabLayout;
    @BindView(R.id.vp_class)
    ViewPager mViewPager;
    @BindView(R.id.v_tab_indicator)
    View mVTabIndicator;

    @BindView(R.id.ll_tb_container)
    LinearLayout mLlTbContainer;

    List<String> titles = new ArrayList<>();
    List<Fragment> mFragments = new ArrayList<>();
    // "" 全部 0待付款 1待发货 2待收货 3待评价 5退货
    String[] types = {"", "0", "1", "2", "3", "5"};
    private int mType; //0全部 1待付款 2待发货 3待收货 4待评价

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, MyOrderListActivity.class);
        intent.putExtra(EXTRA_TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_my_order_list;
    }

    @Override
    protected void initView() {
        mType = getIntent().getIntExtra(EXTRA_TYPE, 0);
    }

    @Override
    protected void initData() {
//        titles.add("全部");
//        titles.add("待付款");
//        titles.add("待发货");
//        titles.add("待收货");
//        titles.add("待评价");
        for (int i = 0; i < 5; i++) {
            titles.add("");
        }
        for (int i = 0; i < titles.size(); i++) {
            OrderListFragment fragment = new OrderListFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type", types[i]);
            fragment.setArguments(bundle);
            mFragments.add(fragment);
        }

        mViewPager.setAdapter(new GeneralFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles));
        mViewPager.setOffscreenPageLimit(titles.size());
        mTabLayout.setupWithViewPager(mViewPager);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int offset = (int) (mVTabIndicator.getWidth() / 4 * (Math.abs(0.5f - positionOffset) * 2f));
                mVTabIndicator.setPadding(offset, 0, offset, 0);
                mVTabIndicator.setTranslationX((position + positionOffset) * mVTabIndicator.getWidth());
                float scale = 1 + .1f * (1 - positionOffset);
                mLlTbContainer.getChildAt(position).setScaleX(scale);
                mLlTbContainer.getChildAt(position).setScaleY(scale);
                if (position + 1 < 5) {
                    float nextScale = 1 + .1f * positionOffset;
                    mLlTbContainer.getChildAt(position + 1).setScaleX(nextScale);
                    mLlTbContainer.getChildAt(position + 1).setScaleY(nextScale);
                }
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < mLlTbContainer.getChildCount(); i++) {
                    if (position == i)
                        ((TextView) mLlTbContainer.getChildAt(i)).setTextColor(ContextCompat.getColor(MyOrderListActivity.this, R.color.colorPrimary));
                    else
                        ((TextView) mLlTbContainer.getChildAt(i)).setTextColor(Color.parseColor("#555555"));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setCurrentItem(mType);
    }
}
